<script setup lang="ts">
import { getArticlesAPI, type ArticleRowItem } from '@/apis/article'
import ArticleItem from '@/components/ArticleItem.vue'
import { ref } from 'vue'

const list = ref<ArticleRowItem[]>([])
const current = ref(1) // 页码
const sorter = ref<'weight_desc' | undefined>('weight_desc') // 排序字段，推荐传 'weight_desc' 最新：可以不传
const isLoading = ref(false) // 是否加载中
const isFinished = ref(false) // 是否加载完成

// 获取文章列表
const getArticles = async () => {
  // 调用文章列表接口
  const res = await getArticlesAPI({
    current: current.value,
    sorter: sorter.value,
    pageSize: 10,
  })
  if (current.value === 1) {
    // 如果是第一页，赋值保存列表
    list.value = res.data.rows
  } else {
    // 如果不是第一页，追加列表
    list.value.push(...res.data.rows)
  }
  // 请求结束了，设置加载状态为 false
  isLoading.value = false
  // 页码累加
  current.value++
  // 判断当前页码是否已超过总页数
  if (current.value > res.data.pageTotal) {
    // 如果页码大于总页数，设置加载完成为 true
    isFinished.value = true
  }
}

// 组件加载时，获取文章列表
// getArticles()

// 点击切换排序
const changeSorter = async (value: typeof sorter.value) => {
  // 更新排序字段
  sorter.value = value
  // 重置页码
  current.value = 1
  // 重置结束状态
  isFinished.value = false
  // 重置加载状态
  isLoading.value = true
  // 根据新的排序字段，重新获取文章列表
  await getArticles()
}
</script>

<template>
  <div class="home-view">
    <!-- 顶部导航栏 -->
    <nav class="my-nav van-hairline--bottom">
      <a
        @click="changeSorter('weight_desc')"
        :class="{ active: sorter === 'weight_desc' }"
        href="javascript:;"
      >
        推荐
      </a>
      <a
        @click="changeSorter(undefined)"
        :class="{ active: sorter === undefined }"
        href="javascript:;"
      >
        最新
      </a>

      <div class="logo">
        <h1 class="logo-text">面经</h1>
      </div>
    </nav>

    <!-- 文章列表 -->
    <van-list
      v-model:loading="isLoading"
      :finished="isFinished"
      finished-text="没有更多了"
      class="article-list"
      @load="getArticles"
    >
      <!-- 文章列表项 -->
      <ArticleItem v-for="item in list" :key="item.id" :article="item" />
    </van-list>
  </div>
</template>

<style lang="scss" scoped>
.home-view {
  margin-bottom: 50px;
  margin-top: 44px;
  .my-nav {
    height: 44px;
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    z-index: 999;
    background: #fff;
    display: flex;
    align-items: center;
    > a {
      color: #999;
      font-size: 14px;
      line-height: 44px;
      margin-left: 20px;
      position: relative;
      transition: all 0.3s;
      &::after {
        content: '';
        position: absolute;
        left: 50%;
        transform: translateX(-50%);
        bottom: 0;
        width: 0;
        height: 2px;
        background: #222;
        transition: all 0.3s;
      }
      &.active {
        color: #222;
        &::after {
          width: 14px;
        }
      }
    }
    .logo {
      flex: 1;
      display: flex;
      justify-content: flex-end;
      .logo-text {
        font-size: 18px;
        font-weight: bold;
        color: var(--van-primary-color);
        margin-right: 16px;
        line-height: 44px;
      }
    }
  }
}
</style>
